<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>采购入库列表</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style type="text/css">
        .layui-form-select dl {
            max-height: 180px;
        }
    </style>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">供应商:</label>
                    <div class="layui-input-inline">
                        <select id="supplierId" name="supplierId">
                            <option value="">请选择供应商</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">申请人:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="applicant" placeholder="请输入申请人" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">单据状态:</label>
                    <div class="layui-input-inline">
                        <select id="state" name="state">
                            <option value="">单据状态</option>
                            <option value="1">入库成功</option>
                            <option value="2">待审核</option>
                            <option value="3">审核中</option>
                            <option value="4">审核通过</option>
                            <option value="5">审核不通过</option>
                            <option value="6">作废</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="purchase-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!--数据表格-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="purchase-table" lay-filter="purchase-table"></table>
    </div>
</div>

<!--表头-->
<script type="text/html" id="purchase-toolbar">
    <button class="pear-btn pear-btn-warming pear-btn-md"
            onclick="addPurchase()">
        <i class="layui-icon layui-icon-add-1"></i>
        新增采购
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="submitReview">
        <i class="layui-icon layui-icon-edit"></i>
        提交审核
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="toVoid">
        <i class="layui-icon layui-icon-delete"></i>
        作废
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="submitHousing">
        <i class="layui-icon layui-icon-util"></i>
        提交入库
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script src="/component/pear/module/tab.js"></script>

<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        window.addPurchase = function () {
            // top.layui.frame.changePageByElement("content","/system/toMake","百度一下",)
            top.layui.admin.jump("36", "新增采购入库", "/system/toMake");
        }


        //加载供应商的查询下拉框
        $.get("/purchase/loadAllSupplier", function (data) {
            let supplierId = $("#supplierId");
            let html = '<option value="">请选择供应商</option>'
            $.each(data.data, function (i, item) {
                html += '<option value="' + item.id + '">' + item.name + '</option>'
            });
            supplierId.html(html);
            form.render();
        });

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '单据ID',
                    field: 'id',
                    width: '140',
                    align: 'center',
                    templet: function (d) {
                        return "<a href='#' onclick='updatePurchase(" + d.id + ")' style='color: blue'>" + d.id + "</a>";
                    }
                },
                {
                    title: '供应商',
                    width: '190',
                    field: 'supplierName',
                    align: 'center'
                },
                {
                    title: '采购批发总额',
                    width: '120',
                    field: 'price',
                    align: 'center',
                },
                {
                    title: '状态',
                    width: '100',
                    field: 'state',
                    align: 'center',
                    templet: function (d) {
                        let state;
                        if (d.state == 1) {
                            state = "<font color=green>入库成功</font>";
                        } else if (d.state == 2) {
                            state = "<font color=blue>待审核</font>";
                        } else if (d.state == 3) {
                            state = "<font color=blue>审核中</font>";
                        } else if (d.state == 4) {
                            state = "<font color=green>审核通过</font>";
                        } else if (d.state == 5) {
                            state = "<font color=red>审核不通过</font>";
                        } else if (d.state == 6) {
                            state = "<font color=red>作废</font>";
                        }
                        return state;
                    }
                },
                {
                    title: '申请人',
                    width: '100',
                    field: 'applicant',
                    align: 'center',
                },
                {
                    title: '入库操作人',
                    width: '100',
                    field: 'operation',
                    align: 'center',
                },
                {
                    title: '入库时间',
                    width: '160',
                    field: 'wareTime',
                    align: 'center',
                },
                {
                    title: '创建时间',
                    width: '160',
                    field: 'createTime',
                    align: 'center',
                }
            ]
        ]

        var tableIns = table.render({
            elem: '#purchase-table',
            url: '/purchase/loadAllPurchasePage',
            page: true,
            cols: cols,
            toolbar: '#purchase-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print'],
            response: {
                statusCode: 200
            },
            done: function (res, curr, count) {
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            },
        });

        //表头监听
        table.on('toolbar(purchase-table)', function (obj) {
            if (obj.event === 'submitReview') {
                submitReview(obj);
            } else if (obj.event === 'toVoid') {
                toVoid(obj);
            } else if (obj.event === 'submitHousing') {
                submitHousing(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        //表格监听
        form.on('submit(purchase-query)', function (data) {
            table.reload('purchase-table', {
                where: data.field
            })
            return false;
        });

        window.updatePurchase = function (data) {
            top.layui.admin.jump("37", "采购入库修改", "/system/toUpdateMake/" + data);
        }

        //提交审核
        function submitReview(obj) {
            let ids = common.checkField(obj, 'id');
            let states = common.checkField(obj, 'state');
            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定提交审核?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/purchase/submitReview/" + ids + "/" + states,
                    dataType: 'json',
                    type: 'POST',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });

        }

        //作废
        function toVoid(obj) {
            let ids = common.checkField(obj, 'id');
            let states = common.checkField(obj, 'state');
            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定作废?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/purchase/toVoidPurchase/" + ids + "/" + states,
                    dataType: 'json',
                    type: 'POST',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });

        }

        //提交入库
        function submitHousing(obj) {
            let ids = common.checkField(obj, 'id');
            let states = common.checkField(obj, 'state');
            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定提交入库?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/purchase/submitPurchase/" + ids + "/" + states,
                    dataType: 'json',
                    type: 'POST',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        //刷新表格
        window.refresh = function (param) {
            table.reload('purchase-table');
        }

    })
</script>
</body>
</html>
